//
//  DebugItemManager.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/12.
//

#import "DebugItemManager.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UIImage+DebugManager.h"
#import "DebugNetWorkManager.h"
#import "DebugNotificationManager.h"
#import "DebugKeyValue.h"
#import "DebugManagerUtil.h"


#import "DebugNetWorkViewController.h"
#import "DebugAppInfoViewController.h"
#import "DebugUserDefaultController.h"
#import "DebugH5WebViewController.h"
#import "DebugSandBoxViewController.h"
#import "DebugNotificationViewController.h"
#import "DebugRabbitMQViewController.h"

#import "DebugLogWindow.h"
#import "DebugPathLogWindow.h"

@interface DebugItemManager ()
///内置item
@property(nonatomic,strong,readwrite) NSArray *mainItems;
///自定义item
@property(nonatomic,strong,readwrite) NSArray *customItems;
///头部开关item
@property(nonatomic,strong) NSMutableArray<NSDictionary *> *switchItems;
@end

@implementation DebugItemManager


NSString *const itemImageStr = @"itemImageStr";
NSString *const itemTitleStr = @"itemTitleStr";
NSString *const ifdissMissMainStr = @"ifdissMissMainStr";
NSString *const itemClickBlockStr = @"itemClickBlockStr";
NSString *const switchItemStr = @"switchItemStr";
NSString *const switchItemBlock = @"switchItemBlock";
NSString *const switchItemNotifName = @"switchItemNotificationName";

static DebugItemManager *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;
}

+ (instancetype)sharedInstance
{
    if (_instance == nil) {
        _instance = [[DebugItemManager alloc] init];
    }
    return _instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        //MARK:开关
        [self addSwitchItemDetail:@"网络抓包" switchItemNotificationName:@"k_dbmanager_networking_record" itemClickBlock:^BOOL(BOOL result) {
            [DebugNetWorkManager sharedInstance].isRecordNetWorking = result;
            return result;
        }];
        
        [self addSwitchItemDetail:@"监听通知" switchItemNotificationName:@"k_dbmanager_notification_record" itemClickBlock:^BOOL(BOOL result) {
            [DebugNotificationManager sharedInstance].isRecordNotification = result;
            return result;
        }];
        [self addSwitchItemDetail:@"显示边框" switchItemNotificationName:@"k_dbmanager_border_show" itemClickBlock:^BOOL(BOOL result) {
            [DebugManagerUtil sharedInstance].debug_borderShow = result;
            return result;
        }];
        
        //MARK:组件
        [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_appinfo"] itemTitle:@"应用信息" itemClickBlock:^(void (^ _Nonnull callBack)(UIViewController * _Nonnull, NSInteger, BOOL), void (^ _Nonnull dissMissMain)(void)) {
            DebugAppInfoViewController *vc = [[DebugAppInfoViewController alloc] init];
            callBack(vc,0,YES);
        }];
        [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_network"] itemTitle:@"网络请求" itemClickBlock:^(void (^ _Nonnull callBack)(UIViewController * _Nonnull, NSInteger, BOOL), void (^ _Nonnull dissMissMain)(void)) {
            DebugNetWorkViewController *vc = [[DebugNetWorkViewController alloc] init];
            callBack(vc,0,YES);
        }];
        [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_userdefatult"] itemTitle:@"UserDefault" itemClickBlock:^(void (^callBack)(UIViewController *viewController, NSInteger type, BOOL animated), void (^dissMissMain)(void)) {
            
            DebugUserDefaultController *vc = [[DebugUserDefaultController alloc] init];
            callBack(vc,0,YES);
        }];
        [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_h5web"] itemTitle:@"H5任意门" itemClickBlock:^(void (^callBack)(UIViewController *viewController, NSInteger type, BOOL animated), void (^dissMissMain)(void)) {
            
            DebugH5WebViewController *vc = [[DebugH5WebViewController alloc] init];
            callBack(vc,0,YES);
        }];
        [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_sandBox"] itemTitle:@"沙盒浏览器" itemClickBlock:^(void (^callBack)(UIViewController *viewController, NSInteger type, BOOL animated), void (^dissMissMain)(void)) {
            
            DebugSandBoxViewController *vc = [[DebugSandBoxViewController alloc] initWithFileUrl:nil title:@"沙盒浏览器"];
            callBack(vc,0,YES);
        }];
        [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_sandBox"] itemTitle:@"通知监听" itemClickBlock:^(void (^callBack)(UIViewController *viewController, NSInteger type, BOOL animated), void (^dissMissMain)(void)) {
            DebugNotificationViewController *vc = [[DebugNotificationViewController alloc] init];
            callBack(vc,0,YES);
        }];
        
        if (NSClassFromString(@"MQManager")) {
            [self includeMQ];
        }
        if (NSClassFromString(@"LogManager")) {
            [self includeLog];
        }
        if (NSClassFromString(@"YKPathLog")) {
            [self includePathLog];
        }
        if (NSClassFromString(@"LookinObject")) {
            [self includeLookin];
        }
    }
    return self;
}

- (void)addCustomItem:(UIImage * _Nonnull)itemImage itemTitle:(NSString *)itemTitle itemClickBlock:(void(^)(void(^callBack)(UIViewController *viewController, NSInteger type, BOOL animated),void(^dissMissMain)(void)))itemClickBlock
{
    if (itemClickBlock) {
        NSMutableArray *array = [self.customItems mutableCopy];
        NSDictionary *dic = @{itemImageStr:itemImage?:[[UIImage alloc]init],itemTitleStr:itemTitle?:@"",itemClickBlockStr:itemClickBlock};
        [array addObject:dic];
        self.customItems = [array copy];
        
        [self reloadMainVC];
    }else{
        NSAssert(NO,@"itemClickBlock should not be nil");
    }
}

- (void)addSystemItem:(UIImage * _Nonnull)itemImage itemTitle:(NSString *)itemTitle itemClickBlock:(void(^)(void(^callBack)(UIViewController *viewController, NSInteger type, BOOL animated),void(^dissMissMain)(void)))itemClickBlock
{
    if (itemClickBlock) {
        NSMutableArray *array = [self.mainItems mutableCopy];
        NSDictionary *dic = @{itemImageStr:itemImage?:[[UIImage alloc]init],itemTitleStr:itemTitle?:@"",itemClickBlockStr:itemClickBlock};
        [array addObject:dic];
        self.mainItems = [array copy];
    }else{
        NSAssert(NO,@"itemClickBlock should not be nil");
    }
}

- (void)getItemDetail:(NSIndexPath *)indexPath detailBlock:(void(^)(UIImage *itemImage,NSString *itemTitle,void(^itemClickBlock)(void(^callBack)(UIViewController *viewController, NSInteger type, BOOL animated),void(^dissMissMain)(void))))detailBlock
{
    NSDictionary *dic = [NSDictionary dictionary];
    switch (indexPath.section) {
        case 0:
        {
            ///开关模块不触发点击事件
            return;
        }break;
        case 1:
        {
            dic = self.mainItems[indexPath.row];
        }break;
        case 2:
        {
            dic = self.customItems[indexPath.row];
        }break;
        default:
            
            break;
    }
    UIImage *itemImage = dic[itemImageStr]?:[[UIImage alloc] init];
    NSString *itemTitle = dic[itemTitleStr]?:@"";
    void(^itemClickBlock)(void(^)(UIViewController *viewController, NSInteger type, BOOL animated),void(^dissMissMain)(void)) = dic[itemClickBlockStr];
    if(detailBlock)
    {
        detailBlock(itemImage,itemTitle,itemClickBlock);
    }
}
- (void)getSwitchItemDetail:(NSIndexPath *)indexPath detailBlock:(void(^)(NSString *switchItemName,NSString *switchItemNotificationName,BOOL(^itemBlock)(BOOL result)))detailBlock
{
    NSDictionary *dic = self.switchItems[indexPath.row];
    NSString *itemTitle = dic[switchItemStr]?:@"";
    NSString *switchItemNotificationName = dic[switchItemNotifName]?:@"";
    BOOL(^itemClickBlock)(BOOL result) = dic[switchItemBlock];
    if (detailBlock) {
        detailBlock(itemTitle,switchItemNotificationName,itemClickBlock);
    }
}

- (void)addSwitchItemDetail:(NSString * _Nonnull)switchName switchItemNotificationName:(NSString * _Nonnull)switchItemNotificationName itemClickBlock:(BOOL(^)(BOOL result))itemClickBlock;
{
    if (itemClickBlock) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:switchName forKey:switchItemStr];
        [dic setObject:itemClickBlock forKey:switchItemBlock];
        [dic setObject:switchItemNotificationName forKey:switchItemNotifName];
        [self.switchItems addObject:[dic copy]];
    }else{
        NSAssert(NO,@"itemClickBlock should not be nil");
    }
}

- (NSArray *)mainItems
{
    if(!_mainItems)
    {
        NSMutableArray *array = [NSMutableArray array];
        _mainItems = array;
    }
    return _mainItems;
}

//引入了MQmanager模块
- (void)includeMQ
{
    [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_rbmq"] itemTitle:@"RabbitMQ" itemClickBlock:^(void (^callBack)(UIViewController *viewController, NSInteger type, BOOL animated), void (^dissMissMain)(void)) {
        DebugRabbitMQViewController *vc = [[DebugRabbitMQViewController alloc] init];
        callBack(vc,0,YES);
    }];
}

//引入LogModule模块
- (void)includeLog
{
    [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_log"] itemTitle:@"RabbitMQ" itemClickBlock:^(void (^callBack)(UIViewController *viewController, NSInteger type, BOOL animated), void (^dissMissMain)(void)) {
        if ([DebugLogWindow sharedInstance].isHidden) {
            [[DebugLogWindow sharedInstance] show];
        }else
        {
            [[DebugLogWindow sharedInstance] dissMiss];
        }
    }];
}

- (void)includePathLog
{
    [self addSystemItem:[UIImage debugmanager_imageNamed:@"ic_main_item_pahlog"] itemTitle:@"埋点记录" itemClickBlock:^(void (^callBack)(UIViewController *viewController, NSInteger type, BOOL animated), void (^dissMissMain)(void)) {
        if ([DebugPathLogWindow sharedInstance].isHidden) {
            [[DebugPathLogWindow sharedInstance] show];
        }else
        {
            [[DebugPathLogWindow sharedInstance] dissMiss];
        }
    }];
}

/// 引入Lookin模块
- (void)includeLookin
{
    [self addSystemItem:[UIImage debugmanager_imageNamed:@"Lookin_Logo"] itemTitle:@"Lookin" itemClickBlock:^(void (^callBack)(UIViewController *viewController, NSInteger type, BOOL animated), void (^dissMissMain)(void)) {
        dissMissMain();
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_3D" object:nil];
    }];
}

- (void)reloadMainVC
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadMainVC" object:@{@"isDebug":@(YES).stringValue}];
}

- (NSArray *)customItems
{
    if(!_customItems)
    {
        _customItems = [NSMutableArray array];
    }
    return _customItems;
}
- (NSMutableArray<NSDictionary *> *)switchItems
{
    if (!_switchItems) {
        _switchItems = [NSMutableArray array];
    }
    return _switchItems;
}

@end
